package net.csdn.business.gateway.service.impl;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.domain.common.RouteEvent;
import net.csdn.business.common.oauth2.model.entity.GatewayRouteInfo;
import net.csdn.business.common.oauth2.service.IGatewayRouteInfoService;
import net.csdn.business.gateway.handler.GatewayRouteHandler;
import net.csdn.business.gateway.service.IRedisReceiverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: zhangyalei
 * @Date: 2022/12/15 21:33
 * @Description:
 */
@Slf4j
@Service
public class RedisReceiverServiceImpl implements IRedisReceiverService {


    @Autowired
    private GatewayRouteHandler gatewayRouteHandler;

    @Autowired
    private IGatewayRouteInfoService gatewayRouteInfoService;

    @Override
    public void routeChangeMsg(String message) {
        log.info("------routeChangeMsg receive msg------:{}",message);
        RouteEvent routeEvent=JSONObject.parseObject(message, RouteEvent.class);
        Integer type=routeEvent.getType();
        String routeId=routeEvent.getRouteId();
        GatewayRouteInfo routeInfo=null;
        switch (type){
            case 1:
                routeInfo=gatewayRouteInfoService.selectByRouteId(routeId);
                gatewayRouteHandler.saveRoute(routeInfo);
                break;
            case 2:
                routeInfo=gatewayRouteInfoService.selectByRouteId(routeId);
                gatewayRouteHandler.updateRoute(routeInfo);
                break;
            case 3:
                gatewayRouteHandler.deleteRoute(routeId);
                break;
        }
    }
}
